<?php

namespace App\Http\Controllers\Api;

use App\Activity;
use App\ActivityJoin;
use App\Http\Services\CommonManager;
use App\Http\Services\ToolsManager;
use App\Http\Services\UserManager;
use App\Orders;
use App\User;
use App\VipInfo;
use App\VipPackage;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    public function buildOrder(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录!','data' => 'no_login']);

        $activityId = $request->get("activity_id");
        $phone      = $request->get('phone');
        $realname   = $request->get('realname');
        $company    = $request->get('company');
        $position   = $request->get('position');
        $email      = $request->get('email');
        $remark     = $request->get('remark');
        $info       = $request->get('info');
        $vipPackage = !is_null($request->get('vip_package_id')) ? $request->get('vip_package_id') : 0;
        $num        = !is_null($request->get('num')) ? $request->get('num') : 1;
        $type       = !is_null($request->get('type')) ? $request->get('type') : 'default';

        if($type == 'default'){
            /** @var Activity $thisActivity */
            $thisActivity = Activity::whereId($activityId)->whereStatus(1)->first();
            if(!$thisActivity)
                return response()->json(['level' => 'error','message' => '活动已下架']);

            if(strtotime($thisActivity->start_time) <= time())
                return response()->json(['level' => 'error','message' => '活动已开始，不能报名了']);

            if(strtotime($thisActivity->end_time) <= time())
                return response()->json(['level' => 'error','message' => '活动时间已结束']);

            $isJoinActivity = ActivityJoin::whereUserId($user->id)->whereActivityId($thisActivity->id)->whereStatus(1)->first();
            if($isJoinActivity)
                return response()->json(['level' => 'error','message' => '您已经报名过了']);

            if(!is_array($info))
                return response()->json(['level' => 'error','message' => '数据类型错误']);

            if(!empty($thisActivity->sign_up_table)){
                $signUpTAble = json_decode($thisActivity->sign_up_table);
            }else{
                $signUpTAble = [];
            }

            if(is_array($signUpTAble) && in_array('realname',$signUpTAble)){
                if(!empty($info[0])){
                    $realname = $info[0];
                }else{
                    $realname = '';
                }

                if(is_null($realname) || $realname == '')
                    return response()->json(['level' => 'error','message' => '请填写姓名']);
            }else{
                $realname = '';
            }

            if(is_array($signUpTAble) && in_array('phone',$signUpTAble)) {
                if(!empty($info[1])){
                    $phone = $info[1];
                }else{
                    $phone = '';
                }

                if(is_null($phone) || $phone == '')
                    return response()->json(['level' => 'error','message' => '请填写手机号']);

                $phoneVerification = CommonManager::phoneVerification($phone);
                if(!$phoneVerification)
                    return response()->json(['level' => 'error','message' => '手机号格式不正确']);
            }else{
                $phone = '';
            }

            if(is_array($signUpTAble) && in_array('company',$signUpTAble)){
                if(!empty($info[2])){
                    $company = $info[2];
                }else{
                    $company = '';
                }

                if(is_null($company) || $company == '')
                    return response()->json(['level' => 'error','message' => '请填写公司名称']);
            }else{
                $company = '';
            }

            if(is_array($signUpTAble) && in_array('position',$signUpTAble)){
                if(!empty($info[3])){
                    $position = $info[3];
                }else{
                    $position = '';
                }

                if(is_null($position) || $position == '')
                    return response()->json(['level' => 'error','message' => '请填写职位']);
            }else{
                $position = '';
            }

            if(is_array($signUpTAble) && in_array('email',$signUpTAble)){
                if(!empty($info[4])){
                    $email = $info[4];
                }else{
                    $email = '';
                }

                if(is_null($email) || $email == '')
                    return response()->json(['level' => 'error','message' => '请填写邮箱']);
            }else{
                $email = '';
            }

            if(is_array($signUpTAble) && in_array('remark',$signUpTAble)){
                if(!empty($info[5])){
                    $remark = $info[5];
                }else{
                    $remark = '';
                }

                if(is_null($remark) || $remark == '')
                    return response()->json(['level' => 'error','message' => '请填写备注']);
            }else{
                $remark = '';
            }


            $buildOrder = new Orders();
            $buildOrder->user_id       = $user->id;
            $buildOrder->order_num     = ToolsManager::makeBillNum();
            $buildOrder->activity_id   = $thisActivity->id;
            $buildOrder->type          = 'activity';
            $buildOrder->m_amount      = $thisActivity->cost;
            $buildOrder->s_amount      = $thisActivity->cost;
            $buildOrder->save();

            $buildJoinActivity = new ActivityJoin();
            $buildJoinActivity->user_id                                               = $user->id;
            $buildJoinActivity->activity_id                                           = $thisActivity->id;
            if(!empty($realname)){
                $buildJoinActivity->realname                                              = $realname;
            }
            if(!empty($phone)){
                $buildJoinActivity->phone                                                 = $phone;
            }
            if(!empty($company)){
                $buildJoinActivity->company                                               = $company;
            }
            if(!empty($position)){
                $buildJoinActivity->position                                              = $position;
            }
            if(!empty($email)){
                $buildJoinActivity->email                                                 = $email;
            }
            if(!empty($remark)){
                $buildJoinActivity->remark                                                = $remark;
            }
            $buildJoinActivity->order_num                                             = $buildOrder->order_num;
            $buildJoinActivity->save();

            if($thisActivity->cost <= 0){
                if($thisActivity->is_review == 1){
                    $buildJoinActivity->status = 2;
                    $buildJoinActivity->save();

                    return response()->json(['level' => 'success', 'message' => '报名提交成功，请等待审核通过']);
                }else{
                    $buildJoinActivity->status = 1;
                    $buildJoinActivity->save();

                    return response()->json(['level' => 'success', 'message' => '报名成功']);
                }
            }
        }elseif($type == 'vip'){
            $vipPackage = VipPackage::whereId($vipPackage)->whereStatus(1)->first();
            if(!$vipPackage)
                return response()->json(['level' => 'error','message' => '该套餐不存在']);

            $vipInfo = VipInfo::whereUserId($user->id)->first();
            if($vipInfo && $vipInfo->status != 1)
                return response()->json(['level' => 'error','message' => '用户得vip状态异常']);

            if($vipInfo && $vipInfo->vip_package_id != $vipPackage->id && strtotime($vipInfo->end_time) > time())
                return response()->json(['level' => 'error','message' => '您已绑定其他套餐']);

            if($num <= 0)
                return response()->json(['level' => 'error','message' => '套餐数量最小为1']);

            $buildOrder = new Orders();
            $buildOrder->user_id          = $user->id;
            $buildOrder->order_num        = ToolsManager::makeBillNum();
            $buildOrder->vip_package_id   = $vipPackage->id;
            $buildOrder->vip_package_num  = $num;
            $buildOrder->type             = 'vip';
            $buildOrder->m_amount         = $vipPackage->free;
            $buildOrder->s_amount         = $vipPackage->free;
            $buildOrder->save();
        }else{
            return response()->json(['level' => 'error','message' => '订单类型不正确']);
        }

        return response()->json(['level' => 'success','message' => '订单生成成功','data' => $buildOrder]);
    }
}
